//
//  WDJLunPanController.m
//  wdj
//
//  Created by 蓝鳍互娱 on 2024/7/8.
//

#import "WDJLunPanController.h"
#import "WDJLunPanView.h"
#import "WDJLunPanEditController.h"
@interface WDJLunPanController ()<CAAnimationDelegate>
@property (nonatomic,strong) WDJLunPanView *lunPanView;
@property (nonatomic,strong) CABasicAnimation *lunPanAnimation;
@property (nonatomic,assign) BOOL isLunPanANimation;
@property (nonatomic,strong) UIButton * lunPanCenterBtn;
@end

@implementation WDJLunPanController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = true;
    self.isLunPanANimation = false;
    [self wdjCreatView];
    [self crearLunPanAnimation];
}
- (void)wdjCreatView{
    UIImageView * worldLunPanImgView = [[UIImageView alloc] init];
    worldLunPanImgView.image = [WDjWholeData wholeImageNamed:@"wdj_LunPanBg"];
    worldLunPanImgView.userInteractionEnabled = true;
    [self.view addSubview:worldLunPanImgView];
    [worldLunPanImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    UIButton * lunPanNavBackBtn = [[UIButton alloc] init];
    [lunPanNavBackBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_NavBack"] forState:UIControlStateNormal];
    [lunPanNavBackBtn addTarget:self action:@selector(lunPanNavBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lunPanNavBackBtn];
    [lunPanNavBackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:24]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:12]+[WDjWholeData wholeTopSafeH]));
    }];
    UIButton * lunPanNavEditBtn = [[UIButton alloc] init];
    [lunPanNavEditBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_NavEdit"] forState:UIControlStateNormal];
    [lunPanNavEditBtn addTarget:self action:@selector(lunPanNavEdit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lunPanNavEditBtn];
    [lunPanNavEditBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:24]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:12]+[WDjWholeData wholeTopSafeH]));
    }];
    UILabel * lunPanNavTitleLab = [[UILabel alloc] init];
    lunPanNavTitleLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:34] weight:UIFontWeightRegular];
    lunPanNavTitleLab.textColor = [WDjWholeData wholeColorHexString:@"#FFFFFF"];
    lunPanNavTitleLab.text = @"决定轮盘";
    [self.view addSubview:lunPanNavTitleLab];
    [lunPanNavTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lunPanNavBackBtn);
        make.centerX.equalTo(self.view);
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:50]));
    }];
    UIImageView * worldLunPanHeadImgView = [[UIImageView alloc] init];
    worldLunPanHeadImgView.image = [WDjWholeData wholeImageNamed:@"wdj_LunPanTitle"];
    worldLunPanHeadImgView.userInteractionEnabled = true;
    [self.view addSubview:worldLunPanHeadImgView];
    [worldLunPanHeadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:175]+[WDjWholeData wholeTopSafeH]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:484]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:79]));
    }];
    self.lunPanView = [[WDJLunPanView alloc] initWithFrame:CGRectMake(([WDjWholeData wholeScreewW] - [WDjWholeData wholeScreewRatio:863])/2, [WDjWholeData wholeScreewRatio:300]+[WDjWholeData wholeTopSafeH], [WDjWholeData wholeScreewRatio:863], [WDjWholeData wholeScreewRatio:863])];
    [self.view addSubview:self.lunPanView];
    self.lunPanView.lunPans = [WDJPublicData defaultManager].lunPans;
//    [self.lunPanView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
//        make.height.equalTo(@([WDjWholeData wholeScreewRatio:863]));
//        make.width.equalTo(@([WDjWholeData wholeScreewRatio:863]));
//        make.top.equalTo(@([WDjWholeData wholeScreewRatio:231]+[WDjWholeData wholeTopSafeH]));
//    }];
    self.lunPanCenterBtn = [[UIButton alloc] init];
    [self.lunPanCenterBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_LunPanGo"] forState:UIControlStateNormal];
    [self.lunPanCenterBtn addTarget:self action:@selector(lunPanCenterGo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.lunPanCenterBtn];
    [self.lunPanCenterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.lunPanView);
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:203]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:165]));
        make.top.equalTo(self.lunPanView).offset([WDjWholeData wholeScreewRatio:310]);
    }];
}
- (void)crearLunPanAnimation{
    self.lunPanAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    self.lunPanAnimation.fromValue = @(0);
    self.lunPanAnimation.duration = 5.5;
    self.lunPanAnimation.repeatCount = 1;
    self.lunPanAnimation.autoreverses = NO;
    self.lunPanAnimation.delegate = self;
    // 先加速后减速
    self.lunPanAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
    // 动画结束后显示结束位置
    self.lunPanAnimation.removedOnCompletion = NO;
    self.lunPanAnimation.fillMode = kCAFillModeForwards;
}
- (void)lunPanNavBack{
    [self.navigationController popViewControllerAnimated:true];
}
- (void)lunPanNavEdit{
    WeakSelf
    WDJLunPanEditController *lunPanEditCtrl = [[WDJLunPanEditController alloc] init];
    [self.navigationController pushViewController:lunPanEditCtrl animated:true];
    lunPanEditCtrl.LunPanSaveBlock = ^{
        weakSelf.lunPanView.lunPans = [WDJPublicData defaultManager].lunPans;
    };
}
- (void)lunPanCenterGo{
    if(!self.isLunPanANimation){
        self.isLunPanANimation = YES;
        self.lunPanAnimation.toValue = @(-M_PI*6 - M_PI*((arc4random()%21+1)/21.0));
        [self.lunPanView.layer removeAllAnimations];
        [self.lunPanView.layer addAnimation:self.lunPanAnimation forKey:@"transform"];
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.isLunPanANimation = NO;
}
@end
